package cn.edu.ntu.yyl.service.imlp;

import cn.edu.ntu.yyl.dao.ArticleDao;
import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleCategory;
import cn.edu.ntu.yyl.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao Dao;
    //根据ID查询
    @Override
    public  Article SelectArticleById(Integer id){
        try {
          return Dao.SelectArticleById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //添加文章
    public int AddNewArticle(Article article){

        try {
            //设置文章ID
            article.setId(getmaxid());
            //执行SQL
            Dao.AddNewArticle(article);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //删除文章
    public int DeleteArticle(Article article){

        try {
            //执行SQL
            Dao.DeleteArticle(article);

        } catch (Exception e) {
            return -1;

        }
        return 1;
    }
    //修改文章
    public int UpdateArticle(Article article){
        try {
            //执行SQL
            Dao.UpdateArticle(article);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //查询所有文章
    public List<Article> SelectAllArticle(){
        try {
            //执行SQL
            return Dao.SelectArticle();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //按照分类查询
    public List<Article> SelectArticleByCategory(ArticleCategory articleCategory) {

        try {
            //执行SQL
            return Dao.SelectArticleByCategory(articleCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    //查询作者的文章
    public List<Article> SelectArticleByUserId(Integer id) {
        try {
            //执行SQL
            return Dao.SelectArticleByUserId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //按照时间查询
    public List<Article> SelectArticleByTime(Date before, Date after) {
        try {
            //执行SQL
            return Dao.SelectArticleByTime(before,after);
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }
    //按照标签查询
    public List<Article> SelectArticleByTag(ArticleCategory articleCategory) {

        try {

            //执行SQL
            return Dao.SelectArticleByTag(articleCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    //更新文章的修改时间
    public Integer UpdateArticleEditorTime(Article article) {

        try {

            //执行SQL
            Dao.UpdateArticleEditorTime(article);
        } catch (Exception e) {
            return -1;

        }
        return 1;
    }
    //标题模糊查询
    public List<Article> SelectTitleByKeyword(String value) {

        try {

            //执行SQL
            return  Dao.SelectTitleByKeyword(value);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }
    //内容模糊查询
    public List<Article> SelectContentByKeyword(String value) {

        try {
            //执行SQL
            return Dao.SelectContentByKeyword(value);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }
    //模糊查询
    public List<Article> SelectArticleLike(String value) {
        //初始化
        List<Article> articles = null;

        try {

            //执行SQL
            articles = Dao.SelectArticleLike(value);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return articles;
    }
    //今天
    public List<Article> SelectTodayArticle() {
        //初始化
        List<Article> articles = null;

        try {

            //执行SQL
            articles = Dao.SelectTodayArticle();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return articles;
    }
    //昨天
    public List<Article> SelectYesterdayArticle() {
        //初始化
        List<Article> articles = null;

        try {

            //执行SQL
            articles = Dao.SelectYesterdayArticle();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return articles;
    }
    //七天
    public List<Article> SelectSevenDayArticle() {
        //初始化
        List<Article> articles = null;

        try {
            //getMapper

            //执行SQL
            articles = Dao.SelectSevenDayArticle();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return articles;
    }
    //半个月
    public List<Article> SelectFourteenDayArticle() {
        //初始化
        List<Article> articles = null;
        try {
            articles = Dao.SelectFourteenDayArticle();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return articles;
    }
    //一个月
    public List<Article> SelectMonthArticle() {
        List<Article> articles = null;
        try {
            articles = Dao.SelectMonthArticle();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return articles;
    }
    //获取置顶文章
    public List<Article> SelectAllRecommendArticles() {
        List<Article> articles = null;

        try {
            articles = Dao.SelectAllRecommendArticles();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return articles;
    }
    //增加点击次数
    public int UpdateViews(Article article) {
        try {
            Dao.UpdateViews(article);
        } catch (Exception e) {
            return -1;

        }
        return 1;
    }
    //查询出文章评论数量并更新
    public int UpdateCommentCountById(Article article) {

        try {
            Dao.UpdateCommentCountById(article);
        } catch (Exception e) {
            return -1;

        }
        return 1;
    }
    //查询出点赞次数并更新
    public Integer UpdateGivelikeCount(Article article) {
        try {
            Dao.UpdateGivelikeCount(article);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //分页查询
    public List<Article> SelectArticleSummary(Integer PageNo, Integer PageSize) {
        //初始化
        List<Article> articles = null;
        try {
            articles = Dao.SelectArticleSummary(PageNo,PageSize);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return articles;
    }
    //按照状态查询
    public List<Article> SelectArticleByStatus(String status){
        try {
            return Dao.SelectArticleByStatus(status);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //按照文章状态查询文章数量
    public Integer SelectArticCountleByStatus(String status){
        try {
            return Dao.SelectArticCountleByStatus(status);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public Integer UpdateKeyByID(String key,String value,Integer id){
        try {
            Dao.UpdateKeyByID(key,value,id);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    //获取文章最大ID
    private Integer getmaxid() {
        //初始化
        List<Article> articles = null;
        Integer articleid = -1;
        try {
            articles = Dao.SelectArticle();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取最大ID然后+1便是需要写入的userID
        if (articles != null) {
            for (Article article : articles) {
                if (articleid < article.getId()) {
                    articleid = article.getId();
                }
            }
        } else return -1;
        articleid++;
        return articleid;
    }
}
